package com.itboye.ihomebank.activity.finance;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.HuanKuanJiHuaBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.presenter.JinRongPresenter;
import com.itboye.ihomebank.util.DoubleUtil;
import com.itboye.ihomebank.util.MyTableTextView;

import java.util.Observable;
import java.util.Observer;

/**
 * Created by itboye on 2017/2/17.
 */
public class InvestmentListActivity extends BaseOtherActivity implements Observer{
    private LinearLayout MyTable;
    private RelativeLayout relativeLayout;
    private String[] name={"收款日","应收本息(元)","应收本金(元)","应收利息(元)"};
    private ImageView back;
    HuanKuanJiHuaBean bean;
    JinRongPresenter presenter;
    private String rate_year;
    private String money;
    private String month;
    private String day;
    private String pay_type;
    private String start_time;

    private TextView tvcomon;
    private TextView tvPrice;//本xi
    private TextView tvPricepayable;//本金
    private TextView tvPricepayablelixi;
    @Override
    protected int layout_id() {
        return R.layout.activity_incestment_list;
    }

    @Override
    protected void onInitController() {

    }

    @Override
    protected void onInit() {
        rate_year=getIntent().getStringExtra("rate_year");
        money=getIntent().getStringExtra("money");
        month=getIntent().getStringExtra("month");
        day=getIntent().getStringExtra("day");
        pay_type=getIntent().getStringExtra("pay_type");
        start_time=getIntent().getStringExtra("start_time");
        MyTable = (LinearLayout) this.findViewById(R.id.MyTable);
        initData();
        bean=new HuanKuanJiHuaBean();
        presenter=new JinRongPresenter(this);
        showProgressDialog("数据加载中...",true);
        presenter.onBenJinLiXi(rate_year,money,month,day,pay_type,start_time);

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.back:
                finish();
                break;
        }
    }
    //绑定数据
    private void initData() {
        //初始化标题
        relativeLayout = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.table, null);
        MyTableTextView title = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_1);
        title.setText(name[0]);
        title.setTextColor(getResources().getColor(R.color.gray_01));
        title = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_2);
        title.setText(name[1]);
        title.setTextColor(getResources().getColor(R.color.gray_01));
        title = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_3);
        title.setText(name[2]);
        title.setTextColor(getResources().getColor(R.color.gray_01));
        title = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_4);
        title.setText(name[3]);
        title.setTextColor(getResources().getColor(R.color.gray_01));
        MyTable.addView(relativeLayout);
    }

    /**
     * 表格数据
     * @param bean
     */
    public  void  onHuankuan(HuanKuanJiHuaBean bean){

        tvcomon.setText("共"+bean.getCount()+"期");
        tvPrice.setText(DoubleUtil.doubleTransform(bean.getTotal())+"元");
        tvPricepayable.setText(DoubleUtil.doubleTransform(bean.getCapital())+"元");
        tvPricepayablelixi.setText(DoubleUtil.doubleTransform(bean.getInterest())+"元");

        //初始化内容
        for (int i=0;i<bean.getPlain().size();i++) {
            relativeLayout = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.table, null);
            MyTableTextView txt = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_1);
            txt.setText("第"+bean.getPlain().size()+"期"+"\n"+bean.getPlain().get(i).getTime());
            txt = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_2);
            txt.setText(DoubleUtil.doubleTransform(bean.getPlain().get(i).getTotal()));//本息
            txt = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_3);
            txt.setText(DoubleUtil.doubleTransform(bean.getPlain().get(i).getCapital()));//本金
            txt = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_4);
            txt.setText(DoubleUtil.doubleTransform(bean.getPlain().get(i).getInterest()));//应收利息
            MyTable.addView(relativeLayout);
        }
    }

    @Override
    public void update(Observable observable, Object data) {

        ResultEntity entity=handlerError(data);
        if(entity!=null){
            if(entity.getEventType()==presenter.huan_kuan_success){
                bean= (HuanKuanJiHuaBean) entity.getData();
                Log.d("HuanKuanJiHuaBeans",bean.getCount());
                onHuankuan(bean);
                try {
                    closeProgressDialog();
                }catch (Exception ex){}

            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
    }
}
